﻿using System.IO;
using System.Linq;
using System.Reflection;
using Framework.Core.Dependency.Domain;
using Framework.Core.Dependency.Parser;
using Xunit;
using Xunit.Extensions;

namespace Framework.Tests.Core.Dependency
{
    public class DotNetProjectParserTest
    {
        private readonly DotNetProjectParser target;

        public DotNetProjectParserTest()
        {
            target = new DotNetProjectParser();
        }

        [Theory]
        [InlineData("MUDLibrary.csproj", "MUDLibrary", Language.CSharp, 23, 96)]
        [InlineData("OWSTestOdyssey.csproj", "OWSTestOdyssey", Language.CSharp, 7, 10)]
        [InlineData("MagicLibrary.csproj", "MagicLibrary", Language.CSharp, 6, 72)]
        [InlineData("CTY.DealManagement.COMWrapperScriptControl.vbproj", "CTY.DealManagement.COMWrapperScriptControl", Language.Vb, 8, 3)]
        [InlineData("RCMImport.vbproj", "RCMImport", Language.Vb, 6, 7)]
        public void Should_test_dotnet_parser(string projectName, string expectedProjectName, Language expectedLanguage, int expectedModuleCount, int expectedFilesCount)
        {
            var project = target.Parse(GetProject(projectName), projectName, "pouet");

            Assert.NotNull(project);

            Assert.Equal(expectedProjectName, project.Name);
            Assert.Equal(expectedLanguage, project.Language);
            Assert.Equal(expectedModuleCount, project.Modules.Count());
            Assert.Equal(expectedFilesCount, project.Files.Count);
        }

        [Fact]
        public void Should_have_path_of_modules()
        {
            const string mudlibraryCsproj = "MUDLibrary.csproj";
            var project = target.Parse(GetProject(mudlibraryCsproj), mudlibraryCsproj, "pouet");

            const string expected = "adodb";
            Assert.Contains(expected, project.Modules.Select(x=>x.Name));
            Assert.Equal(@"..\..\..\..\..\..\..\..\SG\meteor\Bin\adodb.dll", project.Modules.First(m=>m.Name == expected).Path);

        }

        [Fact]
        public void Should_have_path_of_modules_for_leacy_csproj()
        {
            var project = target.Parse(GetProject("MagicLibrary.csproj"), "MagicLibrary.csproj", "pouet");

            Assert.Equal("MagicLibrary", project.Name);
            Assert.Equal(".csproj", project.Extension);
            Assert.Equal("MagicLibrary", project.AssemblyName);

            var module = project.Modules.First(m => m.Name == "System.Design");
            Assert.Equal(@"..\..\..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.0.3705\System.Design.dll", module.Path);

        }

        private static Stream GetProject(string ressourceName)
        {
            var project = Assembly.GetExecutingAssembly().GetManifestResourceStream("Framework.Tests.Core.Dependency.DotNetProj." + ressourceName);

            Assert.NotNull(project);

            return project;
        }
    }
}
